package com.cj.controller;

import com.cj.pojo.Article;
import com.cj.pojo.PageBean;
import com.cj.pojo.Result;
import com.cj.pojo.dto.ArticleQueryDTO;
import com.cj.pojo.dto.ArticleSearchDTO;
import com.cj.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/*
 * 文章控制器
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {

    // 注入ArticleService
    @Autowired
    private ArticleService articleService;

    // 添加文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    // 文章列表
    @PostMapping("/list")
    public Result<PageBean<Article>> list(@RequestBody ArticleQueryDTO queryDTO) {
        PageBean<Article> pb = articleService.list(
            queryDTO.getPageNum(),
            queryDTO.getPageSize(),
            queryDTO.getCategoryId(),
            queryDTO.getState(),
            queryDTO.getUserId()
        );
        return Result.success(pb);
    }

    // 搜索文章
    @PostMapping("/search")
    public Result<PageBean<Article>> search(@RequestBody ArticleSearchDTO searchDTO) {
        PageBean<Article> pb = articleService.search(
            searchDTO.getKeyword(),
            searchDTO.getPageNum(),
            searchDTO.getPageSize()
        );
        return Result.success(pb);
    }

    // 阅读文章
    @PatchMapping("/{id}/view")
    public Result view(@PathVariable Long id) {
        articleService.view(id);
        return Result.success();
    }

    // 批量删除文章
    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody List<Long> ids) {
        articleService.batchDelete(ids);
        return Result.success();
    }
}
